package com.powernode.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.powernode.common.Constants;
import com.powernode.utils.WebMvcUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@Api(tags = "获取验证码的接口")
public class CaptchaController {

    @RequestMapping("captcha.do")
    @ApiOperation("获取验证码")
    public void captcha(HttpServletResponse response){
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 40, 1, 20);
        String code = lineCaptcha.getCode();
        WebMvcUtils.getSession().setAttribute(Constants.CAPTCHA_CODE,code);
        try {
            lineCaptcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
